


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import watij.elements.*;
import watij.runtime.ie.*;

/**
 * Servlet implementation class FetchUIElements
 */
public class FetchUIElements_XML extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public FetchUIElements_XML() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter pw = response.getWriter();									
		String urlTest = request.getParameter("urlTest");				//fetching url to be tested and storing it in a string
		try
		{
			IE ie = new IE();
			ie.start(urlTest);
			TextFields textfield = ie.textFields();						//referring all textfields on the page
			Radios radiobutton = ie.radios();							//referring all radiobuttons in the page
			Checkboxes checkbox = ie.checkboxes();						//referring all checkboxes in the page
			SelectLists list = ie.selectLists();						//referring all combo boxes in the page
			Hiddens hidden = ie.hiddens();								//referring all hidden elements in the page
			String XMLResponse = "<?xml version=\"1.0\"?><Components language=\"en_US\">";
			int i = 0;
			for(i=0;i<textfield.length();i++)
			{
				String component = "";
				component += "<component>";
				component += "<ComponentName>" + ie.textField(i).name() + "</ComponentName>";
				component += "<ComponentType>TextField</ComponentType>";
				component += "</component>";
				XMLResponse += component;
			}
			
			for(i=0;i<radiobutton.length();i++)
			{
				String component = "";
				if(i == 0)
				{
					component += "<component>";
					component += "<ComponentName>" + ie.radio(i).name() + "</ComponentName>";
					component += "<ComponentType>Radio</ComponentType>";
					component += "</component>";
					XMLResponse += component;
				}
				else if (i!=0 && !ie.radio(i-1).name().equals(ie.radio(i).name()))
				{
					component += "<component>" + "\n";
					component += "<ComponentName>" + ie.radio(i).name() + "</ComponentName>";
					component += "<ComponentType>Radio</ComponentType>";
					component += "</component>";
					XMLResponse += component;
				}
			}
			
			for(i=0;i<checkbox.length();i++)
			{
				String component = "";
				if(i == 0)
				{
					component += "<component>";
					component += "<ComponentName>" + ie.checkbox(i).name() + "</ComponentName>";
					component += "<ComponentType>CheckBox</ComponentType>";
					component += "</component>";
					XMLResponse += component;
				}
				else if (i!=0 && !ie.checkbox(i).name().equals(ie.checkbox(i).name()))
				{
					component += "<component>";
					component += "<ComponentName>" + ie.checkbox(i).name() + "</ComponentName>";
					component += "<ComponentType>CheckBox</ComponentType>";
					component += "</component>";
					XMLResponse += component;
				}
			}
			
			for(i=0;i<list.length();i++)
			{
				String component = "";
				component += "<component>";
				component += "<ComponentName>" + ie.selectList(i).name() + "</ComponentName>";
				component += "<ComponentType>SelectList</ComponentType>";
				component += "</component>" ;
				XMLResponse += component;				
			}
			
			int count = 0;
			String[] hiddenElements = new String[hidden.length()];
			for(i=0;i<hidden.length();i++)
			{
				String component = "";
				if(count == 0)
				{
					component += "<component>";
					component += "<ComponentName>" + ie.hidden(i).name() + "</ComponentName>";
					component += "<ComponentType>Hidden</ComponentType>";
					component += "</component>";
					XMLResponse += component;
					hiddenElements[count] = ie.hidden(i).name();
					count++;
				}
				
				else if(count != 0)
				{
					boolean occured = false;
					for(int j = 0; j<count ; j++)
					{
						if(!(ie.hidden(i).name().equals(hiddenElements[j])))
							occured = false;
						else if(ie.hidden(i).name().equals(hiddenElements[j]))
							occured = true;
						if(occured)
							break;
					}
					if(occured = false)
					{
						component += "<component>";
						component += "<ComponentName>" + ie.hidden(i).name() + "</ComponentName>";
						component += "<ComponentType>Hidden</ComponentName>";
						component += "</component>";
						XMLResponse += component;
						hiddenElements[count] = ie.hidden(i).name();
						count++;
					}
				}
			}
			XMLResponse += "</Components>";
			response.setContentType("text/xml");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().write(XMLResponse);
			ie.close();
		}
		catch(Exception ex)
		{
			pw.print(ex.getMessage());
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
